package com.lzw.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzw.anno.Log;
import com.lzw.pojo.Building;
import com.lzw.service.admin.BuildingManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/building")
@CrossOrigin(origins = "http://localhost:7070")
public class BuildingManagementController {

    @Autowired
    private BuildingManagementService buildingManagementService;

    //查询楼宇
    @GetMapping("/list")
    public PageInfo<Building> listBuildings(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String name, String address){
        PageHelper.startPage(pageNum,pageSize);
        List<Building> list = buildingManagementService.listBuildings(pageNum, pageSize, name, address);
        return new PageInfo<>(list);
    }

    //添加楼宇
    @Log
    @PostMapping("/add")
    public ResponseEntity<String> addBuilding(@RequestBody Building building) {
        try {
            buildingManagementService.addBuilding(building);
            return ResponseEntity.ok("添加楼宇成功");
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        }
    }

    //根据id批量删除楼宇
    @Log
    @DeleteMapping("/delete/{ids}")
    public String deleteBuilding(@PathVariable List<Integer> ids){
        buildingManagementService.deleteBuilding(ids);
        return "删除楼宇成功";
    }

    //根据id查询楼宇
    @GetMapping("/get/{id}")
    public Building getBuildingById(@PathVariable("id") int id){
        return buildingManagementService.getBuildingById(id);
    }

    //更新楼宇信息
    @Log
    @PutMapping("/update")
    public ResponseEntity<String> updateBuilding(@RequestBody Building building) {
        try {
            buildingManagementService.updateBuilding(building);
            return ResponseEntity.ok("更新楼宇成功");
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        }
    }
}
